<template>
    <div>
        <!-- header -->
        <el-card class="!border-none" shadow="never">
<!--            <el-alert-->
<!--                type="warning"-->
<!--                title="温馨提示：1.库存预警商品为库存不足的销售中商品；2.商品销售状态分为销售中且库存足够时才可下单购买"-->
<!--                :closable="false"-->
<!--                show-icon-->
<!--            />-->
            <el-form ref="formRef" class="mb-[-16px] mt-[15px]" :model="queryParams" :inline="true">
                <el-form-item label="商品货号">
                    <el-input
                        class="ls-input"
                        v-model="queryParams.code"
                        placeholder="请输入商品货号"
                        clearable
                        @keyup.enter="resetPage"
                    />
                </el-form-item>
                <el-form-item label="商品名称">
                    <el-input
                        class="ls-input"
                        v-model="queryParams.name"
                        placeholder="请输入商品名称"
                        clearable
                        @keyup.enter="resetPage"
                    />
                </el-form-item>
<!--                <el-form-item label="商品分类">-->
<!--                    <el-tree-select-->
<!--                        :props="{ children: 'child_cats', label: 'name', value: 'id' }"-->
<!--                        class="flex-1"-->
<!--                        v-model="queryParams.cat_id"-->
<!--                        :data="optionsData.lists.list"-->
<!--                        clearable-->
<!--                        :default-expand-all="true"-->
<!--                        placeholder="请选择分类"-->
<!--                        check-strictly-->
<!--                    />-->
<!--                </el-form-item>-->
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none mt-4" shadow="never">
            <!-- 导航栏 -->
            <el-tabs v-model="selectOrm" class="demo-tabs" @tab-click="handleTabclick">
                <el-tab-pane :label="`全部`" name="" />
                <el-tab-pane :label="`已上架`" name="1" />
                <el-tab-pane :label="`已下架`" name="0" />
                <el-tab-pane :label="`已删除`" name="delete" />
                <div class="flex justify-end" v-if="selectOrm !== 'delete'">
                    <router-link
                            :to="{
                        path: '/eggManage/addEgg'
                    }"
                    >
                        <el-button type="primary" class="w-[84px] h-[30px] mb-[9px] text-[12px]"
                        >新增商品</el-button
                        >
                    </router-link>
                    <!--                <el-button-->
                    <!--                    @click="uploadll"-->
                    <!--                    class="w-[84px] h-[30px] mb-[9px] text-[12px] ml-2"-->
                    <!--                    :disabled="ids.length > 0 ? false : true"-->
                    <!--                    >一键上架</el-button-->
                    <!--                >-->
                    <!--                <el-button-->
                    <!--                    @click="underall"-->
                    <!--                    class="w-[84px] h-[30px] mb-[9px] text-[12px] ml-2"-->
                    <!--                    :disabled="ids.length > 0 ? false : true"-->
                    <!--                    >一键下架</el-button-->
                    <!--                >-->
                    <el-button
                        type="danger"
                        @click="deleteall"
                        class="w-[84px] h-[30px] mb-[9px] text-[12px] ml-2"
                        :disabled="ids.length > 0 ? false : true"
                    >一键删除</el-button>
                </div>
                <!-- 表格 -->
                <el-table
                    size="large"
                    :data="pager.lists"
                    @selection-change="handleSelectionChange"
                >
                    <el-table-column type="selection" width="60" />
                    <el-table-column label="商品编号" prop="code" min-width="200" />
                    <el-table-column label="商品信息" min-width="280">
                        <template #default="{ row }">
                            <div class="flex items-center">
                                <div>
									<el-image
									    style="width: 60px; height: 60px"
									    :src="row.cover"
									    :preview-src-list="[row.cover]"
									    :hide-on-click-modal="true"
									    :preview-teleported="true"
									    :fit="'cover'"
									>
									</el-image>
                                </div>
                                <el-tooltip :content="row.name" placement="top-start">
                                    <div class="ml-[10px] text_hidden">{{ row.name }}</div>
                                </el-tooltip>
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column label="价格" prop="sell_price" min-width="150" >
						<template #default="{ row }">
                            <div>
                                <span>销售价：</span>
                                <span style="color: #2494f2">{{row.price_selling}}</span>
                            </div>
                            <div>
                                <span>市场价：</span>
                                <span style="color: #2494f2">{{row.price_market}}</span>
                            </div>
<!--							<div style="font-size: 12px; color: #2494f2">{{row.first_cat_name}}&nbsp;>&nbsp;{{row.second_cat_name}}</div>-->
						</template>
					</el-table-column>
                    <el-table-column label="库存" prop="stock" min-width="180" >
						<template #default="{ row }">
							<div>
                                <span>累计库存：</span>
                                <span style="color: #2494f2">{{row.stock_total}}</span>
                            </div>
							<div>
                                <span>剩余库存：</span>
                                <span style="color: #2494f2">{{row.stock_sales}}</span>
                                <span>&nbsp;件</span>
                            </div>
							<div>
                                <span>已销售：</span>
                                <span style="color: #2494f2">{{row.stock_sales_y}}</span>
                                <span>&nbsp;件</span>
                            </div>
						</template>
					</el-table-column>
                    <el-table-column label="销量"  min-width="180">
						<template #default="{ row }">
							<div>
                                <span>实际销售：</span>
                                <span style="color: #2494f2">{{row.stock_sales_y}}</span>
                                <span>&nbsp;件</span>
                            </div>
							<div>
                                <span>实际卖出：</span>
                                <span style="color: #2494f2">{{row.stock_sales_count}}</span>
                                <span>&nbsp;件</span>
                            </div>
						</template>
					</el-table-column>
<!--                    <el-table-column label="排序" prop="sort" min-width="80" />-->
                    <el-table-column label="销售状态" min-width="110">
                        <template #default="{ row }">
                            <el-tag
                                class="ml-2"
                                :type="['success', 'danger'][row.status]"
                                >{{['已下架', '销售中'][row.status]}}</el-tag
                            >
                        </template>
                    </el-table-column>
                    <el-table-column label="创建时间" prop="create_at" min-width="200" />
                    <el-table-column v-if="selectOrm === 'delete'" label="操作" min-width="160" fixed="right">
                        <template #default="{ row }">
                            <el-button
                                type="success"
                                link
                                @click="recoverGood(row.id)"
                            >
                                恢复商品
                            </el-button>
                        </template>
                    </el-table-column>
                    <el-table-column v-else label="操作" min-width="160" fixed="right">
                        <template #default="{ row }">
                            <el-button type="primary" link>
                                <router-link
                                    :to="{
                                        path: '/eggManage/addEgg',
                                        query: {
                                            id: row.id,
                                            code: row.code,
                                        }
                                    }"
                                >
                                    编辑
                                </router-link>
                            </el-button>
                            <el-button
                                type="primary"
                                link
                                @click="handleunder(row.id, row.status)"
                                v-if="row.status == 1"
                            >
                                下架
                            </el-button>
                            <el-button
                                type="primary"
                                link
                                @click="handleunder(row.id, row.status)"
                                v-if="row.status == 0"
                            >
                                上架
                            </el-button>
                            <el-button
                                v-if="row.root != 1"
                                type="danger"
                                link
                                @click="handleDelete(row.id)"
                            >
                                删除
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </el-tabs>
            <!-- 分页 -->
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
    </div>
</template>

<script lang="ts" setup>
import { usePaging } from '@/hooks/usePaging'
import { getEggList, delEgg, rollbackDel, quickEdit } from '@/api/eggManage'
import { addGoodsMz } from '@/api/live'
// import { idInfo, liveInfo } from "./data.ts";

import feedback from '@/utils/feedback'
import { getRoutePath } from '@/router'

const selectOrm = ref('');

const queryParams = reactive({
    name: '',
    cateids: '',
    status: '', // 【0：已下架|1：销售中】 不传默认全部
    type:'index', // 【index：普通商品列表|recycle：商品回收站列表】
});

//分页处理
const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: getEggList,
    params: queryParams
});

getLists();

//切换导航栏
const handleTabclick = async () => {
    await nextTick();
    if (selectOrm.value === 'delete') {
        queryParams.type = 'recycle';
        queryParams.status = '';
    } else {
        queryParams.type = 'index';
        queryParams.status = selectOrm.value;
    }
    getLists()
}


const ids = ref<any[]>([]);

const handleSelectionChange = (val: any[]) => {
    ids.value = val.map((item) => item.id);
};

//删除商品
const handleDelete = async (id: number) => {
    await feedback.confirm('确定要删除？');
    await delEgg({ id_str: id });
    getLists()
};

//上下架商品
const handleunder = async (id: number, status: number) => {
    await feedback.confirm(`确定要${status == 0 ? '上架' : '下架'}`);
    await quickEdit({ id: id, field_val: status == 0 ? 1 : 0, field_name: 'status'});
    getLists()
};

//恢复商品
const recoverGood = async (id: number) => {
    await rollbackDel({ id_str:  id});
    getLists()
};
</script>

<style lang="scss" scope>
//TODO
.el-alert--warning.is-light {
    background-color: rgba(246, 248, 255, 1);
    color: rgba(64, 115, 250, 1);
}

.ls-input {
    width: 280px;
}
</style>
